package linker;

import java.util.Iterator;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class rightkey {

    private byte[] fileBuffer = new byte[20971520];//20MB
    private int temp = 0;//缓冲区数组大小
    private String filename = "";//操控的文件的名字
    private String fileAffixs = "";//文件后缀
    private String fileAllName = "";//文件全名
    private String fileAbsolutePath = "";//文件绝对地址

    public rightkey(File file) {
        fileAllName = file.getName();
        fileAbsolutePath = file.getAbsolutePath();
    }

    public rightkey() {

    }

    public void toCut(File file) {

        InputStream ips = null;
        try {
            ips = new FileInputStream(file);
            temp = ips.read(fileBuffer);
            //处理文件后缀
            int pos = 0;
            String name = fileAllName;
            int len = name.length();
            filename = "";
            while (name.charAt(pos) != '.') {
                filename += name.charAt(pos);
                pos++;
            }
            fileAffixs = "";
            while (pos < len) {
                fileAffixs += name.charAt(pos);
                pos++;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ips != null) {
                try {
                    ips.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            file.delete();
        }
    }

    public void toCopy(File f1) {
        temp = 0;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f1);
            temp = fis.read(fileBuffer);//填入缓冲区数组，并且获得大小
            int pos = 0;
            String name = f1.getName();
            int len = name.length();
            filename = "";
            while (name.charAt(pos) != '.') {
                filename += name.charAt(pos);
                pos++;
            }
            fileAffixs = "";
            while (pos < len) {
                fileAffixs += name.charAt(pos);//保存".后缀"
                pos++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    System.out.println("fn=" + filename);
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void toPaste(File dir) {
        //dir为要复制到的文件夹，因此要新建复制的文件对象f2
        FileOutputStream fos = null;
        String[] dirFilesName = dir.list();
        int length = dirFilesName.length;
        String[] dirFns = removeFileAffixs(dirFilesName);
        String regex = "";
        int index = 0;
        for (int i = 0; i < length; i++) {
            if (dirFilesName[i].equals(fileAllName)) {
                index = -1;
                if (filename.endsWith("-副本")) {
                    regex = filename;
                    break;
                } else if (filename.indexOf("-副本") >= 0) {
                    index = filename.indexOf("-副本") + 3;
                    regex = filename.substring(0, index);
                    break;
                } else {
                    filename = filename + "-副本";
                }
                break;
            }
        }
        int num = 0;
        String newfn = regex;
        if (!regex.equals("")) {
            newfn = regex;
            for (int i = 0; i < length; i++) {
                index = -1;
                if (dirFns[i].contains(regex)) {
                    index = dirFns[i].indexOf(regex);
                    if (index == 0) {
                        num++;
                    }
                }
            }
            if (num != 0) {
                newfn = regex + num;
            }
        } else {
            newfn = filename;
            for (int i = 0; i < length; i++) {
                index = -1;
                if (dirFns[i].contains(filename)) {
                    index = dirFns[i].indexOf(regex);
                    if (index == 0) {
                        num++;
                    }
                }
            }
            if (num != 0) {
                newfn = filename + num;
            }
        }


        File f2 = new File(dir.getAbsolutePath() + "/" + newfn + fileAffixs);
        try {
            fos = new FileOutputStream(f2);
            fos.write(fileBuffer, 0, temp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void toRenameOne(File file, String newName) {
        //重命名
        String path = file.getParent();
        File newfile = new File(path, newName);
        file.renameTo(newfile);

    }

    public void toRenameMore(List<File> files, String fn, List<String> faffixs, int serialNumber, int digitsNumber) {
        //serialNumber：起始编号   digitsNumber：位数
        //重命名多选
        int length = files.size();
        String bhaffix = "";
        ArrayList<String> bhaffixs = new ArrayList<>();
        int num = serialNumber;//起始编号
        for (int i = 0; i < length; i++) {
            bhaffix = "";
            bhaffix += num;
            num++;
            bhaffixs.add(bhaffix);
        }
        Iterator<String> it = bhaffixs.iterator();
        ArrayList<String> newbhaffixs = new ArrayList<>();
        int len;
        int rest;
        while (it.hasNext()) {
            String str = it.next();
            len = str.length();
            rest = digitsNumber - len;
            while (rest > 0) {
                str = '0' + str;
                rest--;
            }
            newbhaffixs.add(str);
        }
        String path = files.get(0).getParent();
        String newName = "";
        it = newbhaffixs.iterator();
        Iterator<String> it1 = faffixs.iterator();
        Iterator<File> it2 = files.iterator();
        while (it.hasNext()) {
            newName = fn + it.next() + it1.next();
            File newfile = new File(path, newName);
            it2.next().renameTo(newfile);
        }
    }

    public String[] removeFileAffixs(String[] filesName) {
        //去除文件后缀
        int length = filesName.length;
        String fn = "";
        String[] fns = new String[length];
        for (int i = 0; i < length; i++) {
            String name = filesName[i];
            fn = "";
            int pos = 0;
            int len = name.length();
            while (name.charAt(pos) != '.') {
                fn += name.charAt(pos);
                pos++;
                if (pos == len) {
                    break;
                }
            }
            fns[i] = fn;
        }
        return fns;
    }

    public String getFileAllName() {
        return fileAllName;
    }

    public void setFileAllName(String fileAllName) {
        this.fileAllName = fileAllName;
    }

    public String getFileAbsolutePath() {
        return fileAbsolutePath;
    }

    public void setFileAbsolutePath(String fileAbsolutePath) {
        this.fileAbsolutePath = fileAbsolutePath;
    }
}
